/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts3Action;
import javacommon.util.ResInfo;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.pub.model.PubData;
import com.tech15.pub.model.PubOffice;
import com.tech15.pub.service.PubDataManager;
import com.tech15.pub.service.PubOfficeManager;
import com.tech15.pub.vo.query.PubOfficeQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class PubOfficeAction extends BaseStruts3Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "uptime desc"; 
	
	//forward paths
	protected static final String LIST_JSP= "/pub/pubOffice_list.jsp";
	protected static final String CREATE_JSP = "/pub/pubOffice_add.jsp";
	protected static final String EDIT_JSP = "/pub/pubOffice_edit.jsp";
	protected static final String SHOW_JSP = "/pub/pubOffice_show.jsp";
	protected static final String VIEW_JSP = "/pub/pubOffice_view.jsp";
	protected static final String SEARCH_JSP = "/pub/pub_office.jsp";
	protected static final String DETAIL_JSP= "/pub/pub_office_detail.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pub/list.do";
	
	private PubOfficeManager pubOfficeManager;
	private PubDataManager pubDataManager;
	
	private PubOffice pubOffice;
	java.lang.String id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			pubOffice = new PubOffice();
		} else {
			pubOffice = (PubOffice)pubOfficeManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setPubOfficeManager(PubOfficeManager manager) {
		this.pubOfficeManager = manager;
	}	
	public void setPubDataManager(PubDataManager manager) {
		this.pubDataManager = manager;
	}	
	
	public Object getModel() {
		return pubOffice;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		PubOfficeQuery query = newQuery(PubOfficeQuery.class,DEFAULT_SORT_COLUMNS);
		Page page = pubOfficeManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	public String search() {
		PubOfficeQuery query = newQuery(PubOfficeQuery.class,DEFAULT_SORT_COLUMNS);
		query.setUserid(getLoginInfo().getUserid());
		Page page = pubOfficeManager.findPage(query);
		savePage(page,query);
		
		List<PubData> lbList = pubDataManager.searchByPid(new Long(1));
		getRequest().setAttribute("lbList", lbList);
		
		if(ObjectUtils.isNotEmpty(this.pubOffice.getLb())){
			List<PubData> lb2List = pubDataManager.searchByPid(new Long(this.pubOffice.getLb()));
			getRequest().setAttribute("lb2List", lb2List);
		}
		return SEARCH_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	public String detail() {
		return DETAIL_JSP;
	}
	
	public String view() {
		return VIEW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			pubOffice.setUserid(getLoginInfo().getUserid());
			pubOffice.setFbr(getLoginInfo().getUserid());
			pubOfficeManager.saveOffice(pubOffice,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		if(ObjectUtils.isNotEmpty(pubOffice.getLb())){
			List<PubData> lbList = pubDataManager.searchByPid(new Long(this.pubOffice.getLb()));
			getRequest().setAttribute("lbList", lbList);
		}
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			pubOfficeManager.updateOffice(this.pubOffice,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			pubOfficeManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
}
